package cn.sc.allinpay.allfavour.business.common.adapter;

import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.pai.common.adapter.BaseRecyAdapter;
import cn.pai.common.utils.AmtUtils;
import cn.sc.allinpay.allfavour.R;
import cn.sc.allinpay.allfavour.business.common.data.DistCouponRecord;

/**
 * description：发券流水 适配器
 * author：pany
 * on 2018/7/17 15:02
 */
public class DistTransAdapter extends BaseRecyAdapter<DistTransAdapter.MyViewHoder, DistCouponRecord> {

    private Map<String, Integer> map = new HashMap<>();
    private OnPrintListener onPrintListener;

    public DistTransAdapter(List<DistCouponRecord> datas) {
        super(datas);
    }

    public DistTransAdapter() {
        super();
    }

    @Override
    public int layoutId() {
        return R.layout.item_dist_trans;
    }

    @Override
    public MyViewHoder onCreateViewHolder(View view) {
        return new MyViewHoder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHoder holder, int position, final DistCouponRecord data) {
        if (!map.containsKey(data.getTransDate())) {
            map.put(data.getTransDate(), position);
        }
        if (map.get(data.getTransDate()).compareTo(position) == 0) {
            holder.tvDate.setVisibility(View.VISIBLE);
            holder.tvDate.setText(fomatDate(data.getTransDate()));
        } else {
            holder.tvDate.setVisibility(View.GONE);
        }
        if (0==data.getCouponType()) {
            holder.tvPrice.setText("面额：" + AmtUtils.changeF2Y(data.getCouponAmt())+data.getCouponUnit());
        } else {
            holder.tvPrice.setText(null);
        }
        holder.tvCouponsName.setText(data.getCouponName());
        holder.tvUse.setText("数量:"+data.getCouponNum());
        holder.tvTrsTime.setText("交易时间：" + fomatTime(data.getTransTime()));
        holder.tvOperator.setText("操作员：" + data.getOperatorName());
        holder.tvTransNo.setText("流水号：" + data.getTransNo());
        holder.tvPrint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onPrintListener!=null) {
                    onPrintListener.onPrint(data);
                }
            }
        });
    }

    private String fomatTime(String time) {
        StringBuffer sb = new StringBuffer();
        sb.append(time.substring(0, 2)).append(":").append(time.substring(2, 4)).append(":").append(time.substring(4));
        return sb.toString();
    }

    private String fomatDate(String date) {
        StringBuffer sb = new StringBuffer();
        sb.append(date.substring(0, 4)).append("-").append(date.substring(4, 6)).append("-").append(date.substring(6));
        return sb.toString();
    }

    @Override
    public void clean() {
        map.clear();
        super.clean();
    }

    @Override
    public void change(List list) {
        map.clear();
        super.change(list);
    }

    public void setOnPrintListener(OnPrintListener onPrintListener) {
        this.onPrintListener = onPrintListener;
    }

    public interface OnPrintListener {
        void onPrint(DistCouponRecord data);
    }

    class MyViewHoder extends RecyclerView.ViewHolder {

        AppCompatTextView tvDate;//时间
        AppCompatTextView tvCouponsName;//卡券名称
        AppCompatTextView tvUse;//使用数量或金额
        AppCompatTextView tvPrice;//售价
        AppCompatTextView tvTrsTime;//交易时间
        AppCompatTextView tvOperator;//操作员
        AppCompatTextView tvTransNo;//交易流水
        AppCompatTextView tvPrint;//打印


        public MyViewHoder(View itemView) {
            super(itemView);
            // TODO Auto-generated constructor stub
            tvDate = itemView.findViewById(R.id.tvDate);
            tvCouponsName = itemView.findViewById(R.id.tvCouponsName);
            tvUse = itemView.findViewById(R.id.tvUse);
            tvPrice = itemView.findViewById(R.id.tvPrice);
            tvTrsTime = itemView.findViewById(R.id.tvTrsTime);
            tvOperator = itemView.findViewById(R.id.tvOperator);
            tvTransNo = itemView.findViewById(R.id.tvTransNo);
            tvPrint = itemView.findViewById(R.id.tvPrint);
        }
    }
}
